Developer Documentation

QuickTime 4 API Documentation

3D Graphics Programming with QuickDraw 3D 1.5.4

Previous | QD3D Book | Overview | Chapter Contents | Next |

Creating Lights

When you use any renderer more powerful than the wireframe renderer, you'll want to create and configure a set of lights to provide illumination for the object in the model. As you've seen, QuickDraw 3D provides a number of types of lights, each of which can emit light of various colors and intensities. The function MyNewLights defined in Listing 6 creates a group of lights. It creates an ambient light, a point light, and a directional light. See the chapter "Light Objects" for more details on creating lights.

Listing 6 Creating a group of lights

TQ3GroupObject MyNewLights (void)
{
    TQ3GroupPosition                myGroupPosition;
    TQ3GroupObject                  myLightList;
    TQ3LightData                    myLightData;
    TQ3PointLightData               myPointLightData;
    TQ3DirectionalLightData         myDirLightData;
    TQ3LightObject                  myAmbientLight, myPointLight, myFillLight;
    TQ3Point3D                      pointLocation = { -10.0, 0.0, 10.0 };
    TQ3Vector3D                     fillDirection = { 10.0, 0.0, 10.0 };
    TQ3ColorRGB                     WhiteLight = { 1.0, 1.0, 1.0 };
    /*Set up light data for ambient light.*/
    myLightData.isOn = kQ3True;
    myLightData.brightness = .2;
    myLightData.color = WhiteLight;
    /*Create ambient light.*/
    myAmbientLight = Q3AmbientLight_New(&myLightData);
    if (myAmbientLight == NULL)
        goto bail;
    /*Create a point light.*/
    myLightData.brightness = 1.0;
    myPointLightData.lightData = myLightData;
    myPointLightData.castsShadows = kQ3False;
    myPointLightData.attenuation = kQ3AttenuationTypeLinear;
    myPointLightData.location = pointLocation;
    myPointLight = Q3PointLight_New(&myPointLightData);
    if (myPointLight == NULL)
        goto bail;
    /*Create a directional light for fill.*/
    myLightData.brightness = .2;
    myDirLightData.lightData = myLightData;
    myDirLightData.castsShadows = kQ3False;
    myDirLightData.direction = fillDirection;
    myFillLight = Q3DirectionalLight_New(&myDirLightData);
    if (myFillLight == NULL)
        goto bail;

    /*Create light group and add each of the lights to the group.*/
    myLightList = Q3LightGroup_New();
    if (myLightList == NULL)
        goto bail;
    myGroupPosition = Q3Group_AddObject(myLightList, myAmbientLight);
    Q3Object_Dispose(myAmbientLight);               /*balance the reference count*/
    if (myGroupPosition == 0)
        goto bail;
    myGroupPosition = Q3Group_AddObject(myLightList, myPointLight);
    Q3Object_Dispose(myPointLight);                 /*balance the reference count*/
    if (myGroupPosition == 0)
        goto bail;
    myGroupPosition = Q3Group_AddObject(myLightList, myFillLight);
    Q3Object_Dispose(myFillLight);                  /*balance the reference count*/
    if (myGroupPosition == 0)
        goto bail;

    return (myLightList);
    
bail:
    /*If any of the above failed, then return nothing!*/
    return (NULL);
}

The MyNewLights function is straightforward. It fills out the fields of the relevant data structures ( TQ3LightData , TQ3PointLightData , and TQ3DirectionalLightData ) and calls the appropriate functions to create new light objects using the information in those structures. If successful, it adds those light objects to a group of lights. The group of lights will be added to a view, as shown in the following section.

The MyNewLights function can leak memory.


© 1997 Apple Computer, Inc.

Previous | QD3D Book | Overview | Chapter Contents | Next |